package org.darcy.service.org.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import org.darcy.dao.org.StaffMapper;
import org.darcy.entity.org.Company;
import org.darcy.entity.org.Department;
import org.darcy.entity.org.Staff;
import org.darcy.entity.privilege.SysUser;
import org.darcy.framework.constant.SysConstant;
import org.darcy.framework.generic.GenericServiceImpl;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.service.org.CompanyService;
import org.darcy.service.org.OrgDepartmentService;
import org.darcy.service.org.StaffService;
import org.darcy.service.privilege.SysUserService;
import org.darcy.utils.vo.ReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

@Lazy
@Service
public class StaffServiceImpl extends GenericServiceImpl<Staff> implements StaffService {

	@Lazy
	@Autowired
	private CompanyService companyService;

	@Lazy
	@Autowired
	private OrgDepartmentService departmentService;

	@Lazy
	@Autowired
	private SysUserService SysUserService;

	@Autowired
	private StaffMapper mapper;

	@Override
	public ReturnVo<Staff> importStaff(List<Staff> personals) {
		if (CollectionUtils.isNotEmpty(personals)) {
			List<String> codes = new ArrayList<>();
			Set<String> deptCodes = new HashSet<>();
			Set<String> companyCodes = new HashSet<>();
			personals.forEach(personal -> {
				codes.add(personal.getCode());
				deptCodes.add(personal.getDeptCode());
			});
			LambdaQueryWrapper<Staff> personalLambdaQueryWrapper = new LambdaQueryWrapper<>();
			personalLambdaQueryWrapper.in(Staff::getCode, codes);
			List<Staff> oldDbPersonals = this.getList(personalLambdaQueryWrapper);
			LambdaQueryWrapper<Company> companyLambdaQueryWrapper = new LambdaQueryWrapper<>();
			companyLambdaQueryWrapper.in(Company::getCode, companyCodes);

			LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
			departmentLambdaQueryWrapper.in(Department::getCode, deptCodes);
			List<Department> departmentList = departmentService.getList(departmentLambdaQueryWrapper);

			Map<String, Department> departmentMap = departmentList.stream()
					.collect(Collectors.toMap(Department::getCode, department -> department));
			Map<String, Staff> oldDbPersonalMap = oldDbPersonals.stream()
					.collect(Collectors.toMap(Staff::getCode, personal -> personal));
			List<Staff> newPersonals = new ArrayList<>();
			personals.forEach(personal -> {
				if (personal.getDeptId() != null) {
					Department department = departmentMap.get(personal.getDeptCode());
					if (department != null) {
						personal.setDeptId(department.getId());
						personal.setDeptName(department.getName());
					}
				}
				if (!oldDbPersonalMap.containsKey(personal.getCode())) {
					newPersonals.add(personal);
					this.insert(personal);
				} else {
					Staff oldPersonal = oldDbPersonalMap.get(personal.getCode());
					convert(oldPersonal, personal);
					newPersonals.add(oldPersonal);
					this.update(oldPersonal);
				}
			});
		}
		return null;
	}

	void convert(Staff oldPersonal, Staff newPersonal) {
		if (oldPersonal != null && newPersonal != null) {
			oldPersonal.setCode(newPersonal.getCode());
			oldPersonal.setJobGradeCode(newPersonal.getJobGradeCode());
			oldPersonal.setName(newPersonal.getName());
			oldPersonal.setPositionCode(newPersonal.getPositionCode());
			oldPersonal.setSex(newPersonal.getSex());
		}
	}

	@Override
	public List<Staff> getListByCodes(List<String> codes) {
		List<Staff> personals = null;
		if (CollectionUtils.isNotEmpty(codes)) {
			LambdaQueryWrapper<Staff> personalQueryWrapper = new LambdaQueryWrapper<>();
			personalQueryWrapper.in(Staff::getCode, codes).eq(Staff::getDelFlag, SysConstant.DEL_FLAG_1);
			personals = this.getList(personalQueryWrapper);
		}
		return personals;
	}

	@Override
	public Staff getByCode(String code) {
		LambdaQueryWrapper<Staff> personalQueryWrapper = new LambdaQueryWrapper<>();
		personalQueryWrapper.eq(Staff::getCode, code).eq(Staff::getDelFlag, SysConstant.DEL_FLAG_1);
		return this.getOne(personalQueryWrapper);
	}

	@Override
	public Staff insert(Staff record) {
		super.insert(record);
		// 增加用户信息
		SysUser user = SysUserService.addUser(record);
		record.setUid(user.getId());
		super.update(record);
		return record;
	}

	@Override
	public Staff getDetail(Integer id) {
		Staff record = mapper.selectById(id);
		return record;
	}

	@Override
	public PageData<Staff> page(PageParam<Staff> param) {
		IPage<Staff> queryPage = new Page<Staff>(param.getQuery().getPageNum(), param.getQuery().getPageSize());
		IPage<Staff> data = mapper.getPage(queryPage, param.getEntity());
		PageData<Staff> pageData = new PageData<Staff>(data, param);
		return pageData;
	}

}
